<?php


namespace app\admin\controller;

use think\Db;
use think\Validate;

/**
 * 新闻
 * @package app\admin\controller
 */
class News extends Base
{
    /**
     * 新闻列表
     */
    public function news_list()
    {
        $title = I('title/s', '');
        $time = I('time/s', '');
        $p = I('p', 0);
        $size = I('size', 15);
        $where = ['status' => 1];
        if ($title) {
            $where['names'] = ['like', "%" . $title . "%"];
        }

        if ($time) {
            $where['add_time'] = ['like', $time . "%"];
        }

        $start_page = $p * $size;
        $count = $db = Db::name('news')->where($where)->count();
        $db = Db::name('news')->where($where)->order('id desc')->limit($start_page, $size)->select();
        $this->returnMsg('1', '', ['list' => $db, 'sum' => $count]);
    }

    /**
     * 新闻详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function news_msg()
    {
        $id = input('id',0);
        $db = Db::name('news')->where(['id'=>$id,'status'=>1])->find();
        $this->returnMsg('1', '', $db);
    }

    /**
     * 添加新闻
     */
    public function add_news()
    {
//        $add_data = I('.');
        $add_data = $this->body_data;
        unset($add_data['token']);
        $rule = [
            'names' => 'require|length:1,50',
            'names_e' => 'require|length:1,200',
        ];

        $msg = [
            'names.require' => '请输入新闻标题',
            'names.length' => '请输入1-50字的新闻标题',
            'names_e.require' => '请输入新闻英文名字',
            'names_e.length' => '请输入1-100字的新闻英文名字',
        ];

//        var_dump(json_encode($add_data));
        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $names = $add_data['names'];
        $have_db = Db::name('news')->where(['names' => $names,'status'=>1])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的新闻标题', []);
        }
        $add_data['add_time'] = date("Y-m-d H:i:s",time());
        $db = Db::name('news')->insert($add_data);
        $this->returnMsg(1, '添加成功', $db);
    }

    /**
     * 编辑新闻
     */
    public function edit_news()
    {
//        $add_data = I('.');
        $add_data = $this->body_data;
        unset($add_data['token']);

        $rule = [
            'names' => 'require|length:1,50',
            'names_e' => 'require|length:1,200',
        ];

        $msg = [
            'names.require' => '请输入新闻标题',
            'names.length' => '请输入1-50字的新闻标题',
            'names_e.require' => '请输入新闻英文名字',
            'names_e.length' => '请输入1-100字的新闻英文名字',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $cid = $add_data['id'];

        $news_db = Db::name('news')->where(['id' => $cid,'status'=>1])->find();
        if(!$news_db) {
            $this->returnMsg(-1, '没有找到新闻数据', []);
        }

        $names = $add_data['names'];
        $have_db = Db::name('news')->where(['names' => $names,'status'=>1, 'id' => ['neq', $cid]])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的新闻标题', []);
        }

        $db = Db::name('news')->where(['id' => $cid])->update($add_data);
        $this->returnMsg(1, '修改成功', $db);
    }

    /**
     * 删除新闻
     */
    public function delete_news()
    {
        $nid = input('id');
        Db::name('news')->where(['id' => $nid])->update(['status' => 2]);
        $this->returnMsg(1, '删除成功');
    }

}